package ar.com.utn.openqsb.lip.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import ar.com.utn.openqsb.commons.util.LanguageUtil;
import ar.com.utn.openqsb.lip.LIPModule;
import ar.com.utn.openqsb.lip.core.Exercise;
import ar.com.utn.openqsb.lip.core.LIPException;
import ar.com.utn.openqsb.lip.core.LIPManager;

public class MenuBarFactory
{

    private static String lang = "ar.com.utn.openqsb.lip.lang.lang";

    public static void createMenu(final LIPModule mainFrame, final JDesktopPane desktop)
    {
        JMenuBar jmb = new JMenuBar();
        // Agregar items dentro de la barra menu
        JMenu menuFile = new JMenu(LanguageUtil.getLocalizedMessage(lang, "menu_problem"));
        jmb.add(menuFile);
        // New archivo
        JMenuItem optionFileNew = new JMenuItem(LanguageUtil.getLocalizedMessage(lang, "menu_new"));
        optionFileNew.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                new NewExerciseDialog(mainFrame, desktop);
            }
        });
        menuFile.add(optionFileNew);

        // Open archivo
        JMenuItem openFileNew = new JMenuItem(LanguageUtil.getLocalizedMessage(lang, "menu_open"));
        openFileNew.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                JFileChooser fileChooser = new JFileChooser();
                int rVal = fileChooser.showOpenDialog(mainFrame);
                if (rVal == JFileChooser.APPROVE_OPTION)
                {
                    try
                    {
                        Exercise descriptor = LIPManager.getInstance().open(fileChooser.getSelectedFile());
                        mainFrame.newWindow(descriptor.getName());
                    } catch (LIPException lipe)
                    {
                        JOptionPane.showMessageDialog(mainFrame, lipe.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    }
                } else
                {
                    System.out.println("You pressed cancel");
                }

            }
        });
        menuFile.add(openFileNew);

        // Agregar menu a la ventana principal
        mainFrame.setJMenuBar(jmb);
    }
}
